package com.taikang.hmp.frame.user.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.service.impl.BaseServiceImpl;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.frame.customer.bean.CustomerInfo;
import com.taikang.hmp.frame.user.bean.AuthUserInfo;
import com.taikang.hmp.frame.user.service.IUserService;

public class UserServiceImpl extends BaseServiceImpl implements IUserService {
	
	public String getCustomerIdByUid(String uid) {
		if(StringUtils.isEmpty(uid)){
			throw new TKException("uid must not be null!");
		}
		
		List<CustomerInfo> customerIdList = super.getNewDao().queryForList("user_space.getCustomer_uid", uid);
		if (customerIdList == null || customerIdList.isEmpty() || StringUtils.isEmpty(customerIdList.get(0).getCustomer_id()))		{
			throw new TKException("custumer id is not exist");
		}
		
		return customerIdList.get(0).getCustomer_id();
	}
	
	
	
	public void saveBasicInfo(Dto dto) {
		//
		String uid = dto.getAsString("uid");
		AuthUserInfo aui = new AuthUserInfo();
		aui.setUser_id(uid);
		aui.setUser_type(dto.getAsString("usertype"));
		super.getNewDao().update("user_space.update_authUserInfo", aui);
		//
		AuthUserInfo authUserInfo = (AuthUserInfo)super.getNewDao().queryForObject("user_space.authInfo_uid", uid);
		CustomerInfo ci = new CustomerInfo();
		ci.setCustomer_id(authUserInfo.getCustom_id());
		ci.setHeight(dto.getAsString("height"));
		ci.setWeight(dto.getAsString("weight"));
		ci.setBreakfirst(dto.getAsString("breakfirst"));
		ci.setLunch(dto.getAsString("Lunch"));
		ci.setDinner(dto.getAsString("dinner"));
		ci.setSleep(dto.getAsString("sleep"));
		ci.setSugar(dto.getAsString("sugar"));
		ci.setHemoglobin(dto.getAsString("hemoglobin"));
		ci.setOperater(uid);
		ci.setSource_cd(dto.getAsString("requestsource"));
		String sugarafter = "0";
		if(dto.getAsString("sugarafter") != null){
			sugarafter = dto.getAsString("sugarafter");
		}
		ci.setSugarafter(sugarafter);
		ci.setIs_init_plan("0");
		super.getNewDao().update("user_space.update_customerInfo", ci);
	}
	
	

	public SessionBean getCustomerBasicInfo(String uid, SessionBean sessionBean) {
		if(sessionBean!=null){
			sessionBean = new SessionBean();
		}
		List<AuthUserInfo> info  = super.getNewDao().queryForList("user_space.authInfo_uid", uid);
		if(info==null || info.isEmpty()){
			throw new TKException("userid:"+uid+"无对应用户信息");
		}
		List<CustomerInfo> info1 = super.getNewDao().queryForList("user_space.getCustomer_uid", uid);
		if(info1==null || info1.isEmpty()){
			throw new TKException("userid:"+uid+"无对应客户信息");
		}
		sessionBean.setAuthUserInfo(info.get(0));
		sessionBean.setCustomerInfo(info1.get(0));
		return sessionBean;
	}
}
